<template>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>基本资料</span>
      </div>
      <!-- 表单 -->
      <el-form
        :model="userForm"
        :rules="userFormRules"
        ref="userFormRef"
        label-width="100px"
      >
        <el-form-item label="登录名称" prop="user_name">
          <el-input v-model="userForm.user_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" prop="user_nickname">
          <el-input
            v-model="userForm.user_nickname"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" prop="user_email">
          <el-input v-model="userForm.user_email"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="userForm.code"><el-button slot="append" :disabled="num !== 0 ? true : false" @click="sendCode">{{!num ? '获取验证码' : num + '秒后再获取'}}</el-button></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitFn">提交修改</el-button>
          <el-button @click="resetFn">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
</template>

<script>

import { updateUserInfoAPI, sendCodeAPI } from '@/api'
export default {
  name: 'UserInfo',
  data () {
    return {
      userForm: {
        user_id: this.$store.state.userInfo.user_id,
        user_name: this.$store.state.userInfo.user_name, // 默认值用登录后获取到的用户名
        user_nickname: '',
        user_email: '',
        code: ''
      },
      num: 0,
      // 表单的验证规则对象
      userFormRules: {
        user_nickname: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '昵称必须是1-10位的非空字符串', trigger: 'blur' }
        ],
        user_email: [
          { required: true, message: '请输入用户邮箱', trigger: 'blur' },
          { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 4, max: 6, message: '验证码格式不正确', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 提交修改
    submitFn () {
      this.$refs.userFormRef.validate(async valid => {
        if (valid) {
          const { data: res } = await updateUserInfoAPI(this.userForm)
          if (res.status !== 0) return this.$message.error(res.message)
          this.$message.success(res.message)
          this.$store.dispatch('getUserInfoActions')
          this.$refs.userFormRef.resetFields()
        } else {
          return false
        }
      })
    },
    // 重置按钮
    resetFn () {
      this.$refs.userFormRef.resetFields()
    },
    // 发送验证码
    sendCode () {
      this.num = 6
      if (this.num !== 0) {
        this.getCode()
        const s = setInterval(() => {
          if (this.num > 0) {
            this.num -= 1
          } else {
            clearInterval(s)
          }
        }, 1000)
      }
    },
    async getCode () {
      const { data: res } = await sendCodeAPI(this.userForm.user_email)
      console.log(res)
    }
  }

}
</script>

  <style lang="less" scoped>
  .el-form {
    width: 500px;
  }
  </style>
